package com.qiyuan.essay.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.qiyuan.common.core.page.TableDataInfo;
import com.qiyuan.common.core.domain.PageQuery;
import com.qiyuan.common.utils.StringUtils;
import com.qiyuan.essay.domain.BlogType;
import com.qiyuan.essay.domain.bo.BlogTypeBo;
import com.qiyuan.essay.domain.vo.BlogTypeVo;
import com.qiyuan.essay.mapper.BlogTypeMapper;
import com.qiyuan.essay.service.IBlogTypeService;
import org.springframework.stereotype.Service;
import com.qiyuan.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 文章类型Service业务层处理
 *
 * @author ruoyi
 * @date 2022-04-16
 */
@Service
public class BlogTypeServiceImpl extends ServicePlusImpl<BlogTypeMapper, BlogType, BlogTypeVo> implements IBlogTypeService {

    @Override
    public BlogTypeVo queryById(Integer id){
        return getVoById(id);
    }

    @Override
    public TableDataInfo<BlogTypeVo> queryPageList(BlogTypeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BlogType> lqw = buildQueryWrapper(bo);
        Page<BlogTypeVo> result = pageVo(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public List<BlogTypeVo> queryList(BlogTypeBo bo) {
        LambdaQueryWrapper<BlogType> lqw = buildQueryWrapper(bo);
        return listVo(lqw);
    }

    private LambdaQueryWrapper<BlogType> buildQueryWrapper(BlogTypeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BlogType> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getTypeName()), BlogType::getTypeName, bo.getTypeName());
        lqw.eq(StringUtils.isNotBlank(bo.getTypeKey()), BlogType::getTypeKey, bo.getTypeKey());
        lqw.eq(bo.getStatus() != null, BlogType::getStatus, bo.getStatus());
        return lqw;
    }

    @Override
    public Boolean insertByBo(BlogTypeBo bo) {
        BlogType add = BeanUtil.toBean(bo, BlogType.class);
        validEntityBeforeSave(add);
        boolean flag = save(add);
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    @Override
    public Boolean updateByBo(BlogTypeBo bo) {
        BlogType update = BeanUtil.toBean(bo, BlogType.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(BlogType entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Integer> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
